package com.tianji.learning.config;

import com.baomidou.mybatisplus.extension.plugins.handler.TableNameHandler;
import com.baomidou.mybatisplus.extension.plugins.inner.DynamicTableNameInnerInterceptor;
import com.tianji.learning.utils.TableInfoContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class MybtisConfiguration {

    @Bean
    public DynamicTableNameInnerInterceptor dynamicTableNameInnerInterceptor() {
        //声明一个map,用于存储TableNameHandler
        Map<String, TableNameHandler> tableNameHandlerMap = new HashMap<>();
        //存储一个TableNameHandler,用来替换表明,替换方式就是从
        //threadLocal中取出来 - 如果没有说明操作的就是原表
        tableNameHandlerMap.put("points_board",
                (sql, tableName) ->
                        TableInfoContext.getInfo() != null ? TableInfoContext.getInfo() : "points_board");
        return new DynamicTableNameInnerInterceptor(tableNameHandlerMap);
    }
}
